<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/12
 * Time: 10:09
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Question extends Base
{
    public function index()
    {
        if (request()->isAjax()) {
            extract(input());
            $map = array();
            $page = input('page') ? input('page') : 1;
            $limit = input("limit") ? input("limit") : 10;// 获取总条数;
            if (isset($keyword) && $keyword != "") {
                $map['a.question_title'] = ['like', "%" . $keyword . "%"];
            }
            if (isset($grade) && $grade != "") {
                $map['a.question_grade_id'] = $grade;
            }
            if (isset($subject) && $subject != "") {
                $map['a.question_subject_id'] = $subject;
            }
            $count = Db::table('up_question_bank')->alias('a')
                ->join('up_grade ug', 'ug.grade_id = a.question_grade_id', 'left')
                ->join('up_subject us', 'us.subject_id = a.question_subject_id', 'left')
                ->where($map)
                ->count();
            $list = Db::table('up_question_bank')->alias('a')
                ->join('up_grade ug', 'ug.grade_id = a.question_grade_id', 'left')
                ->join('up_subject us', 'us.subject_id = a.question_subject_id', 'left')
                ->where($map)
                ->field('a.*,ug.grade_name,us.subject_name')
                ->order('question_is_top asc, question_time desc')
                ->page($page, $limit)
                ->select();//计算总页面
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $list]);
        }
        $grade = Db::table('up_grade')->select();
        $subject = Db::table('up_subject')->order('subject_sort asc')->select();
        $this->assign('grade',$grade);
        $this->assign('subject',$subject);
        return $this->fetch();
    }
    //添加
    public function add(){
        if(Request::instance()->isPost()){
            $question_title = input('param.question_title');
            $question_subject_id = input('question_subject_id');
            $question_grade_id = input('question_grade_id');
            $question_type = input('question_type');
            $question_price = input('question_price');
            $question_status = input('question_status');
            $data = [
                'question_title' => $question_title,
                'question_subject_id' => $question_subject_id,
                'question_grade_id' => $question_grade_id,
                'question_type' => $question_type,
                'question_price' => $question_price,
                'question_status' => $question_status,
                'question_time' => date('Y-m-d H:i:s'),
                'question_from' => 2,
            ];
            $res = Db::table('up_question_bank')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $grade = Db::table('up_grade')->select();
        $subject = Db::table('up_subject')->order('subject_sort asc')->select();
        $this->assign('grade',$grade);
        $this->assign('subject',$subject);
        return $this->fetch();
    }

    public function reject(){
        if(request()->isPost()){
            $url = input('param.img_file');
            $file_suf = explode('.',$url);//文件路径
            $suffix = $file_suf[count($file_suf)-1];//后缀名
            $dis = ['doc', 'docx', 'xls', 'xlsx'];
            if(!in_array($suffix, $dis)){
                return json(['code' => 100, 'msg' => '文件格式不正确']);
            }
            $data['q_file_name'] = input('param.q_file_name');
            $data['q_file_url'] = $url;
            $data['q_file_dis'] = $suffix;
            $data['q_file_time'] = date('Y-m-d H:i:s',time());
            $data['q_question_id'] = input('param.id');
            $a = file_get_contents($url);
             $bb = round(strlen($a)/1024,2);
            $data['q_file_size'] = $bb;
            $res = Db::table('up_question_file')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $question_id = input('param.question_id');
        $this->assign('question_id',$question_id);
        return $this->fetch();
    }

    public function detail(){
        $map['question_id'] = input('param.question_id');
        $list = Db::table('up_question_bank')->alias('a')
            ->join('up_grade ug', 'ug.grade_id = a.question_grade_id', 'left')
            ->join('up_subject us', 'us.subject_id = a.question_subject_id', 'left')
            ->where($map)
            ->field('a.*,ug.grade_name,us.subject_name')
            ->find();//计算总页面
        $a =Db::table('up_question_file')->where('q_question_id',input('param.question_id'))->column('q_file_name');
        if(count($a)>0){
            $list['name']= implode(',',$a);
        }else{
            $list['name']= '';
        }
        $this->assign('list',$list);
       return $this->fetch();
    }
    
    /**
     * 上架
     * @return \think\response\Json
     */
    public function questionOn(){
        $question_id = input('id');
        $res = Db::table('up_question_bank')->where('question_id', $question_id)->setField('question_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 200, 'msg' => '上架失败']);
        }
    }
    
    /**
     * 下架
     * @return \think\response\Json
     */
    public function questionDown(){
        $question_id = input('id');
        $res = Db::table('up_question_bank')->where('question_id', $question_id)->setField('question_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 200, 'msg' => '下架失败']);
        }
    }
    
    /**
     * 置顶
     * @return \think\response\Json
     */
    public function setQuestionTop(){
        $question_id = input('id');
        $is_top = Db::table('up_question_bank')->where('question_id', $question_id)->value('question_is_top');
        if($is_top == 1){
            $question_is_top = 2;
        }else{
            $question_is_top = 1;
        }
        $res = Db::table('up_question_bank')->where('question_id', $question_id)->setField('question_is_top', $question_is_top);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}